package com.sharemarking.wa.platform.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharemarking.wa.common.Page;
import com.sharemarking.wa.common.ResponseParams;
import com.sharemarking.wa.common.exception.RequestException;
import com.sharemarking.wa.common.entity.Address;
import com.sharemarking.wa.platform.dto.address.AddressInfoDto;
import com.sharemarking.wa.platform.dto.address.AddressListDto;
import com.sharemarking.wa.platform.mapper.AddressMapper;
import com.sharemarking.wa.platform.mapper.DictMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Service
public class AddressService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private AddressMapper addressMapper;


    public ResponseParams<?> getAddressList() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        int pageIndex = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageIndex")) ? "1" : request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageSize")) ? "10" : request.getParameter("pageSize"));

        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        params.put("pageSize", pageSize);
        params.put("memberId", memberId);

        List<AddressListDto> addressListDtos = addressMapper.getChooseAddress(params);

        return ResponseParams.ok(new Page<>(addressListDtos, pageIndex, pageSize, addressMapper.getChooseAddressTotal(params), addressListDtos.size() == pageSize));
    }


    public ResponseParams<?> addOrEditAddress() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        String addressId = request.getParameter("addressId");
        String province = request.getParameter("province");
        String city = request.getParameter("city");
        String area = request.getParameter("area");
        String address = request.getParameter("address");
        String mobile = request.getParameter("mobile");
        String name = request.getParameter("name");
        String postCode = request.getParameter("postcode");
        String provinceCode = request.getParameter("provinceCode");
        String cityCode = request.getParameter("cityCode");
        String areaCode = request.getParameter("areaCode");


        if (StringUtils.isEmpty(province)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("province"));
        }
        if (StringUtils.isEmpty(city)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("city"));
        }
        if (StringUtils.isEmpty(area)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("area"));
        }
        if (StringUtils.isEmpty(address)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("address"));
        }
        if (StringUtils.isEmpty(name)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("name"));
        }
        if (StringUtils.isEmpty(postCode)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("postCode"));
        }

        Address addressEntity = new Address();
        addressEntity.setAddress(address);
        addressEntity.setArea(area);
        addressEntity.setCity(city);
        addressEntity.setProvince(province);
        addressEntity.setMemberId(memberId);
        addressEntity.setName(name);
        addressEntity.setMobile(mobile);
        addressEntity.setPostCode(postCode);
        addressEntity.setProvinceCode(provinceCode);
        addressEntity.setCityCode(cityCode);
        addressEntity.setAreaCode(areaCode);

        if (StringUtils.isEmpty(addressId)) {
            //插入
            addressEntity.setCreateDate(new Date());
            addressEntity.setIsDefault(0);
            addressMapper.insert(addressEntity);
        } else {
            //更新
            addressEntity.setId(Integer.valueOf(addressId));
            addressEntity.setUpdateDate(new Date());
            addressMapper.updateById(addressEntity);
        }

        return ResponseParams.ok(null);
    }


    public ResponseParams<?> deleteAddress() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        String addressId = request.getParameter("addressId");

        if (StringUtils.isEmpty(addressId)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("addressId"));
        }


        Address address = addressMapper.selectById(addressId);
        if (!address.getMemberId().equals(memberId)) {
            throw RequestException.create(ResponseParams.isDataError("无权限删除"));
        }

        addressMapper.deleteById(addressId);

        return ResponseParams.ok(null);
    }

    public ResponseParams<?> getAddressInfo() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        String addressId = request.getParameter("addressId");

        Address address = addressMapper.selectOne(new QueryWrapper<Address>().eq("member_id",memberId).eq("id",addressId));
        AddressInfoDto addressInfoDto = new AddressInfoDto();

        if (address!=null){
            BeanUtils.copyProperties(address,addressInfoDto);
        }
        return ResponseParams.ok(addressInfoDto);
    }

    public ResponseParams<?> setAddressDefault() {
        Integer memberId = ss.getCurrentUserId();
        String addressId = request.getParameter("addressId");

        Address address = new Address();
        address.setIsDefault(1);
        address.setId(Integer.valueOf(addressId));
        address.setUpdateDate(new Date());
        addressMapper.update(address,new QueryWrapper<Address>().eq("member_id",memberId).eq("id",addressId));
        addressMapper.updateOtherAddressNoDefault(memberId, Integer.valueOf(addressId));

        return ResponseParams.ok(null);
    }
}
